package com.yuncheng.oaApi.service.impl;

import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.oaApi.constant.OaCommonFlowConstant;
import com.yuncheng.oaApi.entity.NkHtHtbggl;
import com.yuncheng.oaApi.mapper.NkHtHtbgglMapper;

import com.yuncheng.oaApi.service.INkHtHtbgglService;

import com.yuncheng.oaApi.utils.GetUuIdUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import utils.DateConversionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 内控-合同编号管理
 * @Author: yuncheng
 * @Date: 2024-06-13
 * @Version: V1.0
 */
@Service
public class NkHtHtbgglServiceImpl extends BaseServiceImpl<NkHtHtbgglMapper, NkHtHtbggl> implements INkHtHtbgglService {





    public String getFhtbh(String fpm){
        NkHtHtbggl nkHtHtbggl = new NkHtHtbggl();


        //序号 （当前最大的序号加1）
        Integer i = baseMapper.selectMaxxh();
        if (i == null || i == 0){
            i = 1;
        }else {
            i += 1;
        }
        nkHtHtbggl.setFxh(i);



        //前缀
        String fqz = "SPHT";
        nkHtHtbggl.setFqz(fqz);

        //中间
        if (fpm.equals(OaCommonFlowConstant.CGPM_HW)){
            fpm = "HW";
        }else if(fpm.equals(OaCommonFlowConstant.CGPM_FW)) {
            fpm = "FW";
        }else if (fpm.equals(OaCommonFlowConstant.CGPM_KT)){
            fpm = "KT";
        }
        nkHtHtbggl.setFzj(fpm);

        //后缀 后缀分两个部位组成  年月+当前的第几个

        //第一部分
        String dybfYYYY = DateConversionUtils.DateToYear(new Date());
        String debfMM = DateConversionUtils.DateToIntegerMonth(new Date());

        String dybf = dybfYYYY + debfMM;
        nkHtHtbggl.setFhz(dybf);


        //第二部分 后缀序号 根据品目 来查询当天最大的序号
        //通过第一部分查询当天最大的后缀序号，如果没有那就设置为1，如果有那就查询中间品目是否相等
        Integer hzxh = 1;
        //通过品目查询出 所有数据 获取最大序号.
        List<NkHtHtbggl> nkHtHtbggls = baseMapper.selectList(new LambdaQueryWrapper<NkHtHtbggl>()
                .eq(NkHtHtbggl::getFzj, fpm));

        //如果不为空
        if (CollectionUtils.isNotEmpty(nkHtHtbggls)){
            List<NkHtHtbggl> nkHtHtbggls1 = baseMapper.selectMaxhzxh(fpm);
            String fhz = nkHtHtbggls1.get(0).getFhz();
            Integer fhzxh = nkHtHtbggls1.get(0).getFhzxh();
            //如果后缀相同 ，那就将 后缀序号加1
            if (fhz.equals(dybf)){
                hzxh = fhzxh + 1;
            }else {
                hzxh = hzxh;
            }
            nkHtHtbggl.setFhzxh(hzxh);

        }else {
            nkHtHtbggl.setFhzxh(hzxh);
        }
        String qb = "";
        //拼接
        if(hzxh < 10){
            qb = (fqz + "-" + fpm + "-" + dybf + "00"+ hzxh);
        }else if(10 <= hzxh && hzxh < 99){
            qb = (fqz + "-" + fpm + "-" + dybf + "0"+ hzxh);
        }

        nkHtHtbggl.setFqp(qb);
        nkHtHtbggl.setId(GetUuIdUtils.ReplaceUuId());
        baseMapper.insert(nkHtHtbggl);


        return qb;

    }



}
